package com.shawn.receiver;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;
import android.util.Log;

import com.shawn.mobliesafe.R;
import com.shawn.service.GPSService;

public class SmsReceiver extends BroadcastReceiver {
    private static MediaPlayer mediaPlayer;
    
    private SharedPreferences sp;
    public SmsReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName componentName = new ComponentName(context,Admin.class);
        Log.e("TAG","短信监听");
        sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        String sp_safenum = sp.getString("safenum","");
        //接受解析短信
        //70汉字一条短信,71汉字两条短信
        Object[] objs = (Object[]) intent.getExtras().get("pdus");
        for (Object obj : objs){
            //解析成SmsMessage
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
            String body = smsMessage.getMessageBody();
            String sender = smsMessage.getOriginatingAddress();
            Log.e("TAG","sp_safenum:" +sp_safenum+"body:"+body+"sender:"+sender);
            
            if(sp_safenum.equals(sender)) {
                if("#*location*#".equals(body)) {
                    Log.e("TAG","GPS追踪");
                    //开启一个服务
                    Intent intent_gps = new Intent(context,GPSService.class);
                    context.startService(intent_gps);
                    abortBroadcast();
                }else if("#*alarm*#".equals(body)) {
                    //播放报警音乐
                    //在播放报警音乐之前,将系统音量设置成最大
                    //声音的管理者
                    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                    //设置系统音量的大小
                    //streamType : 声音的类型
                    //index : 声音的大小   0最小    15最大
                    //flags : 指定信息的标签
                    //getStreamMaxVolume : 获取系统最大音量,streamType:声音的类型
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
                    if(mediaPlayer!=null) {
                        mediaPlayer.release();
                    }
                    mediaPlayer = MediaPlayer.create(context, R.raw.ylzs);
                    //mediaPlayer.setLooping(true);
                    mediaPlayer.start();
                    abortBroadcast();
                }else if("#*wipedata*#".equals(body)) {
                    if (devicePolicyManager.isAdminActive(componentName)) {
                        devicePolicyManager.wipeData(0);//远程删除数据
                    }
                    abortBroadcast();
                }else if("#*lockscreen*#".equals(body)) {
                    //判断超级管理员是否激活
                    if(devicePolicyManager.isAdminActive(componentName)) {
                        devicePolicyManager.lockNow();

                    }
                    abortBroadcast();
                }
            }
        }
    }
}
